package mylab.io.base.util;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import mylab.io.base.Das;
import mylab.io.base.fsm.DasStatus;
import mylab.io.base.model.Msg;
import mylab.io.base.model.impl.SocketEndpoint;
import mylab.io.base.model.Terminal;
import mylab.utils.common.concurrent.RunnableUtil;
import mylab.utils.common.jackson.JacksonUtil;


@Slf4j
@UtilityClass
@SuppressWarnings("all")
public class DasUtil {

    public boolean isValidEndpoint(SocketEndpoint endpoint) {
        return endpoint != null && endpoint.getAddress() != null && !endpoint.getAddress().isUnresolved();
    }

    public void logStatus(Das das, DasStatus status) {
        log.info("### onDasStatusChanged ###\ndas id：{}\nstatus: {}", das.getId(), status.getLabel());
    }

    public void logTerminalConnection(Das das, Terminal terminal, boolean isConnected) {
        log.info("### onTerminalConnectionChanged ###\ndas id:{}\nterminal id:{}\nstatus: {}", das.getId(), terminal.getId(), isConnected);
    }

    public void logException(Das das, Throwable throwable) {
        log.info("### onDasException ###\ndas id：{}\n: {}", das.getId(), throwable.getMessage());
    }

    public void logMsg(Msg msg, boolean isSend, boolean isSync) {
        if (isSend) {
            log.info("{} msg: {}", isSync ? "syncSend" : "asyncSend", JacksonUtil.toJsonString(msg));
        } else {
            log.info("{} msg: {}", isSync ? "syncReceived" : "received", JacksonUtil.toJsonString(msg));
        }
    }

    public void postRunnable(Runnable runnable) {
        RunnableUtil.submit(runnable);
    }

}
